<?php
namespace GRH\Model\View;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;

use GRH\Model\CodeActivite;

class CodeActiviteView extends AbstractTableGateway
{
    protected $table = 'grh_code_activite_view';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new CodeActivite());
        $this->initialize();
    }
    
    public function getAll()
    {
        return $this->select();
    }
    
    public function getSelectList()
    {
        $rows = $this->select();
        $arr = array();
        foreach($arr as $code)
            $arr[$code->code_activite_id] = $code->libelle;
        
        return $arr;
    }
    
    public function get($id)
    {
        $id = (int)$id;
        $row = $this->select(array('code_activite_id' => $id))
                ->current();
        if(!$row)
            throw new \Exception("Impossible de trouver '$id'");
        
        return $row;
    }
}